import {request} from "../../request/index"
Page({
    data:{
        tabs:[
            {
                id:0,
                value:'综合',
                isActive:true
            },
            {
                id:1,
                value:'销量',
                isActive:false
            },
            {
                id:2,
                value:'价格',
                isActive:false
            },
        ],
        goodsList:[]
    },
    //接口耀得参数
    QueryParams:{
        query:"",
        cid:"",
        pagenum:1,
        pagesize:10
    },
    totalPages:1,
    onLoad:function(options){
        //options-----能够获取上一个页面的属性和属性值;
        //可以获取跳转过来时候传递的参数;
        this.QueryParams.cid = options.cid||"";
        this.QueryParams.query = options.query||""
        this.getGoodsList()
    },
    async getGoodsList(){
        const res = await request({
            url:'/goods/search',
            data:this.QueryParams
        })
        console.log(res)
        //获取总条数;
        const total = res.total;
        this.totalPages = Math.ceil(total/this.QueryParams);

        this.setData({
            goodsList:[...this.data.goodsList,...res.goods]
        })

        //关闭下拉刷新的窗口，如果没有调用下拉刷新的窗口，关闭也不会报错
        wx.stopPullDownRefresh()
    },
    //标题点击事件，从子组件中传递过来了一个index
    handleTabsItemChange(e){
       //获取被点击事件的索引
       const {index} = e.detail;
       //修改原数组;
       let {tabs} = this.data;
       tabs.forEach((v,i)=>{
           i===index?v.isActive =true:v.isActive=false
       })
       this.setData({
           tabs
       })
    },
    //页面上滑滚动条触底事件;
    onReachBottom(){
        if(this.QueryParams.pagenum>=this.totalPages){
           wx.showToast({
               title: '没有下一页数据'
           });
        }else{
            this.QueryParams.pagenum++;
            this.getGoodsList()
        }
    },
    onPullDownRefresh(){
        //重置数组
        this.setData({
            goodsList:[]
        })
        //重置页码
        this.QueryParams.pagenum=1;
        this.getGoodsList()
    }
})